package pl.edu.uw.come.mooba.util;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;

/**
 * 
 * 
 *
 * @author Elliotte Rusty Harold "Processing XML with Java" (http://www.cafeconleche.org/books/xmljava/)
 */
public class MyErrorHandler implements ErrorHandler {
    
    // Flag to check whether any errors have been spotted.
    private boolean valid = true;
    
    public boolean isValid() {
      return valid; 
    }
    
    // If this handler is used to parse more than one document, 
    // its initial state needs to be reset between parses.
    public void reset() {
      // Assume document is valid until proven otherwise
      valid = true; 
    }
    
    public void warning(SAXParseException exception) {
      
      System.out.println("Warning: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber() 
       + ", column " + exception.getColumnNumber());
      // Well-formedness is a prerequisite for validity
      valid = false;
      
    }
    
    public void error(SAXParseException exception) {
       
      System.out.println("Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber() 
       + ", column " + exception.getColumnNumber());
      // Unfortunately there's no good way to distinguish between
      // validity errors and other kinds of non-fatal errors 
      valid = false;
      
    }
    
    public void fatalError(SAXParseException exception) {
       
      System.out.println("Fatal Error: " + exception.getMessage());
      System.out.println(" at line " + exception.getLineNumber() 
       + ", column " + exception.getColumnNumber()); 
       
    }
}